import type { Person } from "@/types";
import { defineStore } from "pinia";
import { isRef, ref, toValue } from "vue";

export const usePersonStore = defineStore("persons", () => {

  const persons = ref<Person[]>([])

  function addPerson(person: Person) {

    console.log("add person: ", person, " isref: ", isRef(person))
    persons.value.push(person)
    console.log("persons: ", persons)
  }

  function removePerson(person: Person) {
    persons.value.splice(persons.value.indexOf(person), 1)
  }

  function searchPerson(name: string) {
    persons.value.forEach(person => {
      if (person.name === name) {
        console.log(person)
        return person
      }
    })
  }

  return { persons, addPerson, removePerson, searchPerson }
}
)
